package cc.marcio.systems.seven.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "t_round", uniqueConstraints = { @UniqueConstraint(columnNames = { "id" }) })
@SequenceGenerator(name = "seq_round", sequenceName = "seq_round")
public class Round implements Serializable {

	private static final long serialVersionUID = 3838410376241705282L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_round")
	@Column( unique = true, insertable = false, updatable = false )
	private Long id;
	
	@Column( nullable = false, unique = false, insertable = true, updatable = true, length = 80 )
	private String name;
	
	@Basic
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false)
	private Date start;
	
	@Column( nullable = false, unique = false, insertable = true, updatable = true )
	private Integer ticks;
	
	@Column( nullable = false, unique = false, insertable = true, updatable = true )
	private Integer tickTime;

	@Column( nullable = false, unique = false, insertable = true, updatable = true )
	private Integer currentTick;
	
	private Boolean current;
	
	public Round() {
		this.current = true;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Integer getTicks() {
		return ticks;
	}

	public void setTicks(Integer tickets) {
		this.ticks = tickets;
	}

	public Integer getCurrentTick() {
		return currentTick;
	}

	public void setCurrentTick(Integer currentTick) {
		this.currentTick = currentTick;
	}
	
	public Integer getTickTime() {
		return tickTime;
	}

	public void setTickTime(Integer ticketTime) {
		this.tickTime = ticketTime;
	}
	
	public void setupRound() {
		if( this.current && ( this.currentTick == null || this.currentTick <= this.ticks ) ) {
			Calendar now = Calendar.getInstance();
			long minutes = now.getTime().getTime() - this.start.getTime();
			this.currentTick = new Long(minutes / 3600000).intValue() + 1;
			
			if( this.currentTick.equals(this.ticks) ) {
				this.current = false;
			}
		}	
	}

	public Boolean getCurrent() {
		return current;
	}

	public void setCurrent(Boolean current) {
		this.current = current;
	}
}